/********************************************************************************
* Description: 
* Author: hsqiang
* Create Date: 2023-06-22
********************************************************************************/
#ifndef CONNECTOR_HPP_
#define CONNECTOR_HPP_

#include "socket.hpp"
#include "noncopyable.hpp"
#include "net_event_callback.hpp"

namespace event_framework
{
    class EventLoop;
    class Connector final : Noncopyable
    {
    public:
        // socket_ at local
        Connector(EventLoop& loop_, Socket& socket_) : loop(loop_), socket(socket_) {}
        ~Connector() = default;
        void SetNewConnCallback(NewConnCallback&& func)
        {
            ASSERT(func);
            callback = std::move(func);
        }
        bool Connect(const EndPoint& peer);
        void Fini()
        {
            if (finished) return;
            finished = true;
            socket.Close();
        }
        bool Avaliable() const { return !finished; }
    private:
        bool finished { false };
        EventLoop& loop;
        Socket& socket;
        NewConnCallback callback;
    };
    using ConnectorPtr = std::unique_ptr<Connector>;
}

#endif